package org.javaboy.consumer.controller;

import org.apache.juli.logging.Log;
import org.javaboy.consumer.IHelloService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

@RestController
public class HelloController {

    private static final Logger logger = LoggerFactory.getLogger(HelloController.class);

    /**
     * 服务发现工具，利用这个工具可以去服务注册中心获取到对应的服务信息
     * 这个工具是 Spring Cloud 提供的接口，具体的实现，则在于我们所使用的注册中心
     * 所以，无论我们使用 Eureka 还是 Nacos，这个工具都是一样的
     */
    @Autowired
    DiscoveryClient discoveryClient;

    AtomicInteger count = new AtomicInteger(0);

    @Autowired
    RestTemplate restTemplate;

    @GetMapping("/hello")
    public String hello() throws IOException {
        //根据服务名去查询具体的服务信息
        //因为这个服务可能是集群化部署的
        List<ServiceInstance> providers = discoveryClient.getInstances("provider");
        ServiceInstance instance = providers.get(count.getAndIncrement() % providers.size());
        //获取到 provider 服务的 IP 和端口号
        String host = instance.getHost();
        int port = instance.getPort();
        //现在就有了 provider 服务的 IP 和端口号，就可以拼接出访问地址
        String url = "http://" + host + ":" + port + "/hello";
        //调用 provider 服务
        URL u = new URL(url);
        HttpURLConnection con = (HttpURLConnection) u.openConnection();
        BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));
        String s = br.readLine();
        br.close();
        return s;
    }

    @GetMapping("/hello2")
    public String hello2() throws IOException {
        //根据服务名去查询具体的服务信息
        //因为这个服务可能是集群化部署的
        List<ServiceInstance> providers = discoveryClient.getInstances("provider");
        ServiceInstance instance = providers.get(count.getAndIncrement() % providers.size());
        //获取到 provider 服务的 IP 和端口号
        String host = instance.getHost();
        int port = instance.getPort();
        //现在就有了 provider 服务的 IP 和端口号，就可以拼接出访问地址
        String url = "http://" + host + ":" + port + "/hello";
        //调用 provider 服务
        String s = restTemplate.getForObject(url, String.class);
        return s;
    }

    @GetMapping("/hello3")
    public String hello3(@RequestParam("name") String name) {
        logger.info("name:{}", name);
        //调用 provider 服务，注意这里不能给一个明确的地址，因为 provider 可能是集群化部署的
        //这里给出的其实就是服务名
        String s = restTemplate.getForObject("http://provider/hello", String.class);
        return s + ">>>" + name;
    }

    @Autowired
    IHelloService helloService;

    @GetMapping("/hello4")
    public String hello4() {
        return helloService.hello();
    }

}
